/*
 * Copyright 2025 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.firebase.ai.java

import androidx.concurrent.futures.SuspendToFutureAdapter
import com.google.common.util.concurrent.ListenableFuture
import com.google.firebase.ai.TemplateGenerativeModel
import com.google.firebase.ai.type.FirebaseAIException
import com.google.firebase.ai.type.GenerateContentResponse
import com.google.firebase.ai.type.PublicPreviewAPI
import kotlinx.coroutines.reactive.asPublisher
import org.reactivestreams.Publisher

/**
 * Wrapper class providing Java compatible methods for [TemplateGenerativeModel].
 *
 * @see [TemplateGenerativeModel]
 */
@OptIn(PublicPreviewAPI::class)
public abstract class TemplateGenerativeModelFutures internal constructor() {

  /**
   * Generates new content using the given templateId with the given inputs.
   *
   * @param templateId The ID of server prompt template.
   * @param inputs the inputs needed to fill in the prompt
   * @return The content generated by the model.
   * @throws [FirebaseAIException] if the request failed.
   * @see [FirebaseAIException] for types of errors.
   */
  public abstract fun generateContent(
    templateId: String,
    inputs: Map<String, Any>
  ): ListenableFuture<GenerateContentResponse>

  /**
   * Generates new content as a stream using the given templateId with the given inputs.
   *
   * @param templateId The ID of server prompt template.
   * @param inputs the inputs needed to fill in the prompt
   * @return A [Publisher] which will emit responses as they are returned by the model.
   * @throws [FirebaseAIException] if the request failed.
   * @see [FirebaseAIException] for types of errors.
   */
  public abstract fun generateContentStream(
    templateId: String,
    inputs: Map<String, Any>
  ): Publisher<GenerateContentResponse>

  /** Returns the [TemplateGenerativeModel] object wrapped by this object. */
  public abstract fun getGenerativeModel(): TemplateGenerativeModel

  private class FuturesImpl(private val model: TemplateGenerativeModel) :
    TemplateGenerativeModelFutures() {
    override fun generateContent(
      templateId: String,
      inputs: Map<String, Any>
    ): ListenableFuture<GenerateContentResponse> {
      return SuspendToFutureAdapter.launchFuture { model.generateContent(templateId, inputs) }
    }

    override fun generateContentStream(
      templateId: String,
      inputs: Map<String, Any>
    ): Publisher<GenerateContentResponse> {
      return model.generateContentStream(templateId, inputs).asPublisher()
    }

    override fun getGenerativeModel(): TemplateGenerativeModel {
      return model
    }
  }

  public companion object {

    /**
     * @return a [TemplateGenerativeModelFutures] created around the provided
     * [TemplateGenerativeModel]
     */
    @JvmStatic
    public fun from(model: TemplateGenerativeModel): TemplateGenerativeModelFutures =
      FuturesImpl(model)
  }
}
